package Leetcode.lc001;


import java.util.HashMap;

/*
   两数之和，hash表存储
 */
public class Sloution {
    /**
     *
     * @param nums
     * @param target
     * @return
     */
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                int index=map.get(target-nums[i]);
                int[] ints = new int[2];
                ints[0]=i;
                ints[1]=index;
                return ints;
            }
            map.put(nums[i],i);
        }
        return null;
    }

    public static void main(String[] args) {
        //方法使用对象驱动
          Sloution a1=new Sloution();
          int[] a=new int[]{2,7,11,15};
          int[] ints = a1.twoSum(a, 9);
         for(int i:ints){
             System.out.println(i);
         }
    }
}
